package com.yanqu.road.server.cmd.strongholdwar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_GET_RANK_REWARD, desc = "")
public class CrossStrongholaGetRankRewardCmd extends CrossUnionCommand {
    private int activityId;
    private int conditionId;
    private long userId;
    private String unionUid;
    private int position;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        unionUid = reqMsg.getUnionUid();
        userId = packet.getPlayerId();
        position = reqMsg.getPosition();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int myRank = -1;
        ActivityConditionInfo activityConditionInfo = CrossStrongholdActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null != activityConditionInfo) {
            if (activityConditionInfo.getType() == eGamePlayerEventType.StrongholdWildUnionScoreRank.getValue() ||
                    activityConditionInfo.getType() == eGamePlayerEventType.StrongholdEliminateUnionRank.getValue()) {
                UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, activityConditionInfo.getType(), userId);
                if (unionActivityConditionData == null) {
                    unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, eGamePlayerEventType.StrongholdWildUnionScoreRank.getValue(), userId);
                }
                unionUid = unionActivityConditionData.getUnionUid();
                CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
                reqMsg.setActivityId(activityId);
                reqMsg.setConditionId(conditionId);
                reqMsg.setUnionUid(unionUid);
                position = getUnionPosition(unionActivityConditionData, userId);
            }

            if (activityConditionInfo.getType() == CrossStrongholdActivityMgr.CONDITION_TYPE_WILD_USER || activityConditionInfo.getType() == CrossStrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_USER) {
                myRank = CrossStrongholdRankMgr.getCrossActivityRank(activityId, activityConditionInfo.getType(), serverId, userId);
            } else {
                myRank = CrossStrongholdRankMgr.getCrossActivityUnionRank(activityId, activityConditionInfo.getType(), serverId, unionUid);
            }
        }

        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(conditionId);
        respMsg.setRank(myRank);
        respMsg.setPosition(position);
        respMsg.setUnionId(unionUid);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_STRONGHOLD_GET_RANK_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);


        //荣誉点奖励
        /*ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo != null && activityConditionInfo != null) {
            if (activityConditionInfo.getType() == CrossStrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION) {
                int lowestRank = CrossUnionHonorsActivityMgr.getLowestRewardRank(activityInfo.getServerIdList().size(), eHonorsActivityType.UNION_HONORS_STRONG_HOLD);
                List<String> unionUidTop = CrossStrongholdRankMgr.getCrossActivityUnionRankTop(activityId, activityConditionInfo.getType(), lowestRank);
                //CrossUserMgr.getLogger().debug("----  reward serverId {}", serverId);
                CrossUnionHonorsActivityMgr.add(activityId, unionUidTop, activityInfo.getType(), activityInfo.getChildType(), activityInfo.getServerIdList(),serverId);
            }
        }*/
    }

    @Override
    public void sendData() {

    }

    private int getUnionPosition(UnionActivityConditionData unionActivityConditionData, long userId){
        int position = 0;
        if(null == unionActivityConditionData){
            return position;
        }
        if(null != unionActivityConditionData.getMemberPositionMap()){
            if(unionActivityConditionData.getMemberPositionMap().containsKey(userId)){
                position = unionActivityConditionData.getMemberPositionMap().get(userId);
            }
        }
        if(0 == position){
            position = eUnionPosition.Member.getValue();
            if(unionActivityConditionData.getUnionBaseInfo().getMasterUserId() == userId){
                position = eUnionPosition.Master.getValue();
            }
        }
        return position;
    }
}
